package com.sdkx.wear_os.utils

import android.annotation.SuppressLint
import android.util.Log
import android.widget.Toast
import com.sdkx.wear_os.app.MyApplication

/**
 * 自定义Toast和Log
 */
object ToastLogUtils {
    private var toast: Toast? = null
    fun toastUtil(message: Any?) {
        when (message) {
            is String -> makeToast(message)
            else -> makeToast(message.toString())
        }
    }

    /**
     * Toast
     */
    @SuppressLint("ShowToast")
    private fun makeToast(message: String) {
        if (message.isEmpty()) {
            return
        }
        if (toast == null) {
            toast = Toast.makeText(MyApplication.application, message, Toast.LENGTH_SHORT)
        } else {
            toast?.cancel()
            toast = Toast.makeText(MyApplication.application, message, Toast.LENGTH_SHORT)
            toast?.setText(message)
        }
        toast?.show()
    }

    /**
     * Log
     */
    fun logUtil(TAG: String, content: Any?) {
        if (!isApkInDebug(MyApplication.application)) {
            //保存日志内容
            saveFile("$TAG----$content\n", "Log.txt")
            return
        }
        var contentStr = content.toString()
        while (contentStr.length > 2000) {
            Log.e(TAG, contentStr.substring(0, 2000))
            contentStr = contentStr.substring(2000)
        }
        Log.e(TAG, contentStr)
    }
}
